package edu.tongji.libraryapp.adapter;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import edu.tongji.libraryapp.BookDetailActivity;
import edu.tongji.libraryapp.R;
import edu.tongji.libraryapp.model.Book;

public class SearchListAdapter extends BaseAdapter {

	private ArrayList<Book> searchList;
	private Context appContext;
	private Context activityContext;

	public SearchListAdapter(Context appContext, Context activityContext,
			ArrayList<Book> searchList) {
		this.setAppContext(appContext);
		this.setActivityContext(activityContext);
		this.searchList = searchList;
	}

	@Override
	public int getCount() {
		return searchList.size();
	}

	@Override
	public Object getItem(int position) {
		return searchList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			LayoutInflater mInflater = (LayoutInflater) appContext
					.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
			convertView = mInflater.inflate(R.layout.search_list_item, null);
			TextView bookViewDetail = (TextView) convertView
					.findViewById(R.id.bookViewDetail);
			bookViewDetail.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Intent searchIntent = new Intent();
					searchIntent.setClass(activityContext,
							BookDetailActivity.class);
					activityContext.startActivity(searchIntent);
					((Activity) activityContext).overridePendingTransition(
							R.anim.slide_in_left, R.anim.slide_out_left);
				}
			});
		}
		return convertView;
	}

	public ArrayList<Book> getSearchList() {
		return searchList;
	}

	public void setSearchList(ArrayList<Book> searchList) {
		this.searchList = searchList;
	}

	public Context getAppContext() {
		return appContext;
	}

	public void setAppContext(Context appContext) {
		this.appContext = appContext;
	}

	public Context getActivityContext() {
		return activityContext;
	}

	public void setActivityContext(Context activityContext) {
		this.activityContext = activityContext;
	}

}
